<?php

namespace App\Admin\Controllers;

use App\Models\Notice;
use App\Models\Crousesheet;
use App\Models\Student;
use App\Models\Squad;
use App\Models\Dictation_config;
use App\Models\Attendance_record;
use App\Models\Study_jindu_record;
use Encore\Admin\Auth\Database\Administrator;
use Encore\Admin\Auth\Database\Role;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Http\Request;
use App\Admin\Extensions\ExcelExpoter;
use Maatwebsite\Excel\Facades\Excel;
use DB;

class My_done_noticesController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('admin.my_done_notices'));
            $content->description(trans('admin.list'));
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {

    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {

    }

    public function show($id, Content $content) {
		$noticeInfo = Notice::where('id',$id)->first()->toArray();
        $html = $content
            ->header('事项')
            ->description(trans('admin.detail'))
            ->body(Admin::show(Notice::findOrFail($id), function (Show $show) {
                $show->panel()->tools(function ($tools){
                    $tools->disableDelete();
                    $tools->disableEdit();
                    $tools->disableList();
                });
                $show->id('ID');
                $show->event('事项内容');
                $show->starttime(trans('admin.notice_starttime'));
                $show->endtime(trans('admin.notice_endtime'));

                //不同类型的事项，显示不同的处理
                $notice_id = $show->getModel()->id;
                $notice_type = Notice::where('id',$notice_id)->value('notice_type');
                //dd($notice_type);
                switch ($notice_type) {
                    case '1':
                        $show->attendance_record('考勤记录', function ($line) {
                            $line->setResource('/admin/attendance_record');
                            $line->id('序号')->sortable();
                            $line->student_id(trans('admin.en_name'))->display(function($student_id){
                                return Student::find($this->student_id)->en_name;
                            });
                            $line->attendance_status(trans('admin.attendance_status'))->radio([
                                1 => '正常',
                                2 => '请假',
                                3 => '迟到',
                                4 => '缺勤',
                                5 => '家长代课',
                            ]);
                            //$line->attendance_status(trans('admin.attendance_status'))->editable('select', ['1' => '正常', '2' => '请假', '3' => '迟到', '4' => '缺勤']);
                            $line->attendance_desc(trans('admin.attendance_desc'))->display(function($attendance_desc) {
                                    return str_limit($attendance_desc, 30, '...');
                            })->editable('textarea');
                            $line->paginate(50);
                            $line->disableActions();
                            $line->disableCreateButton();
                            $line->disableFilter();
                            $line->tools(function ($tools) {
                                $tools->batch(function ($batch) {
                                    $batch->disableDelete();
                                });
                            });
                        });
                        break;
                    case '2':
                        $show->test_score_record('测试成绩记录', function ($line) {
                            $line->setResource('/admin/test_score_record');
                            $line->model()->orderBy('id', 'desc');
                            $line->id(trans('admin.ID'))->sortable();
                            $line->student_id(trans('admin.en_name'))->display(function($student_id){
                                return Student::find($this->student_id)->en_name;
                            });
                            $line->test_status(trans('admin.test_status'))->radio([
                                1 => '参加',
                                2 => '未参加',
                            ]);
                            $line->test_score(trans('admin.test_score'))->editable();
                            $line->test_desc(trans('admin.test_desc'))->display(function($test_desc) {
                                    return str_limit($test_desc, 30, '...');
                            })->editable('textarea');
                            $line->paginate(50);
                            $line->disableActions();
                            $line->disableCreateButton();
                            $line->disableFilter();
                            $line->tools(function ($tools) {
                                $tools->batch(function ($batch) {
                                    $batch->disableDelete();
                                });
                            });
                            //$line->disableExport();
                        });
                        break;
                    case '3':
                        $show->jiaxiao_record('家校记录', function ($line) {
                            $line->setResource('/admin/jiaxiao_record');
                            //$line->model()->orderBy('id', 'desc');
                            $line->id('序号')->sortable();
                            $line->student_id(trans('admin.en_name'))->display(function($student_id){
                                return Student::find($this->student_id)->en_name;
                            });
                            $line->jiaxiao_status(trans('admin.jiaxiao_status'))->radio([
                                1 => '准交',
                                2 => '未交',
                                3 => '补交'
                            ]);
                            //$line->jiaxiao_status(trans('admin.jiaxiao_status'))->editable('select', [1 => '准交', 2 => '未交',3 => '补交']);
                            $line->jiaxiao_score(trans('admin.jiaxiao_score'))->editable();
                            $line->jiaxiao_desc(trans('admin.jiaxiao_desc'))->display(function($jiaxiao_desc) {
                                    return str_limit($jiaxiao_desc, 30, '...');
                            })->editable('textarea');
                            $line->paginate(50);
                            $line->disableActions();
                            $line->disableCreateButton();
                            $line->disableFilter();
                            $line->tools(function ($tools) {
                                $tools->batch(function ($batch) {
                                    $batch->disableDelete();
                                });
                            });
                        });
                        break;
                    case '4':
                        $show->dictation_score_record('听写成绩记录', function ($line) {
                            $line->setResource('/admin/dictation_score_record');
                            $line->id(trans('admin.ID'))->sortable();
                            $line->student_id(trans('admin.en_name'))->display(function($student_id){
                                return Student::find($this->student_id)->en_name;
                            });
                            $line->dictation_status(trans('admin.dictation_status'))->radio([
                                1 => '正常',
                                2 => '未参加'
                            ]);
                            //$line->dictation_status(trans('admin.dictation_status'))->editable('select', [1 => '正常', 2 => '未参加']);
                            $line->dictation_score(trans('admin.dictation_score'))->editable();
                            $line->dictation_desc(trans('admin.dictation_desc'))->display(function($dictation_desc) {
                                    return str_limit($dictation_desc, 30, '...');
                            })->editable('textarea');

                            $line->paginate(50);
                            $line->disableActions();
                            $line->disableCreateButton();
                            $line->disableFilter();
                            $line->tools(function ($tools) {
                                $tools->batch(function ($batch) {
                                    $batch->disableDelete();
                                });
                            });
                            //$line->disableExport();
                        });
                        break;
                    case '5':
                        $show->zuoye_record('作业记录', function ($line) {
                            $line->setResource('/admin/zuoye_record');
                            $line->id('序号')->sortable();
                            $line->student_id(trans('admin.en_name'))->display(function($student_id){
                                return Student::find($this->student_id)->en_name;
                            });
                            $line->zuoye_status(trans('admin.zuoye_status'))->radio([
                                1 => '准交',
                                2 => '未交',
                                3 => '补交',
                            ]);
                            //$line->zuoye_status(trans('admin.zuoye_status'))->editable('select', ['1' => '准交', '2' => '未交',3 => '补交']);
                            $line->zuoye_score(trans('admin.zuoye_score'))->editable();
                            $line->zuoye_desc(trans('admin.zuoye_desc'))->display(function($zuoye_desc) {
                                    return str_limit($zuoye_desc, 30, '...');
                            })->editable('textarea');
                            $line->paginate(50);
                            $line->disableActions();
                            $line->disableCreateButton();
                            $line->disableFilter();
                            $line->tools(function ($tools) {
                                $tools->batch(function ($batch) {
                                    $batch->disableDelete();
                                });
                            });
                            //$line->disableExport();

                        });
                        break;
                    case '6':
                        // $show->divider();
                        // $show->study_jindu_record('教学进度',function($line){
                        //     $line->jindu_content('进度');
                        //     $line->panel()->tools(function ($tools){
                        //         $tools->disableDelete();
                        //         $tools->disableEdit();
                        //         $tools->disableList();
                        //     });
                        // });
                        break;
                    default:
                        # code...
                        break;
                }
            }));
		if($noticeInfo['notice_type'] == "6"){
			$jindu_content = DB::table("study_jindu_records")->where([
					"notice_id" => $id
				])->value("jindu_content");
			$jindu_content = json_decode($jindu_content,true);
			$html = $html->body(view("admin.plans.jindu_content",["CT" => $jindu_content]));
		}
		return $html;
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Notice::class, function (Grid $grid) {
            // $tutor_squad_ids = Squad::where('class_teacher',Admin::user()->id)->pluck('id')->toArray();
            // $test_arr = DB::table('test_assigns')->whereIn('squad_id',$tutor_squad_ids)->pluck('id')->toArray();
            // $tutor_crousesheet_ids = Crousesheet::where('tutor_id',Admin::user()->id)->pluck('id')->toArray();
            // $dictation_ids = Dictation_config::whereIn('crousesheet_id',$tutor_crousesheet_ids)->pluck('id')->toArray();
            // $grid->model()->whereIn('status',['2','4'])->where(function($builder) use($tutor_crousesheet_ids,$test_arr,$dictation_ids){
            //     $builder->whereIn('crousesheet_id',$tutor_crousesheet_ids)
            //         ->orWhereIn('test_assign_id',$test_arr)
            //         ->orWhereIn('dictation_config_id',$dictation_ids);
            // });
            $grid->model()->where('starttime','<',date('Y-m-d H:i:s',time()))->whereIn('status',['2','4'])->where('tutor_id',Admin::user()->id);
            $grid->model()->orderBy('operatetime','desc');
            $grid->id(trans('admin.ID'))->sortable();
            $grid->notice_type(trans('admin.notice_type'))->using(['1' => '考勤', '2' => '测试', '3' => '家校', '4' => '听写','5' => '作业','6'=>'教学进度'])->sortable();
            $grid->event(trans('admin.notice_event'))->style('max-width:600px;word-break:break-all;');
            $grid->starttime(trans('admin.notice_starttime'))->sortable();
            $grid->endtime(trans('admin.notice_endtime'))->sortable();
            //$grid->operatetime(trans('admin.notice_operatetime'));
            $grid->status(trans('admin.status'))->display(function($status){
                if ($this->status == 2) {
                    return "<span class='label bg-green'>已完成</span>";
                }
                elseif ($this->status == 4) {
                    return "<span class='label bg-yellow'>补录完成</span>";
                }
                elseif ($this->status == 5) {
                    return "<span class='label bg-navy'>忽略完成</span>";
                }
            })->sortable();
            $grid->operatetime(trans('admin.notice_operatetime'))->sortable();

            $grid->disableCreateButton();
            //$grid->disableFilter();
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->actions(function ($actions) {
                $actions->disableDelete();
                $actions->disableEdit();
            });
            $grid->expandFilter();
            //筛选弹出框字段
            $grid->filter(function ($filter) {
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                $filter->equal('notice_type','类型')->select(['1'=>'考勤','2'=>'测试','3'=>'家校','4'=>'听写','5'=>'作业','6'=>'教学进度']);
                // $filter->scope('考勤', '考勤记录')->where('notice_type', '1');
                // $filter->scope('测试', '测试记录')->where('notice_type', '2');
                // $filter->scope('家校', '家校记录')->where('notice_type', '3');
                // $filter->scope('听写', '听写记录')->where('notice_type', '4');
                // $filter->scope('作业', '作业记录')->where('notice_type', '5');
                // $filter->scope('教学进度', '教学进度记录')->where('notice_type', '6');
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {

    }
}
